<template>
  <div class="user-container">
    <!-- 用户个人资料 -->
    <div class="user-profile">
      <div class="info">
        <van-image round
                   :src="userInfo.photo" />
        <h3 class="name">
          {{userInfo.name}}
          <br />
          <van-tag size="mini">申请认证</van-tag>
        </h3>
      </div>
      <van-row>
        <van-col span="8">
          <p>{{userInfo.follow_count}}</p>
          <p>动态</p>
        </van-col>
        <van-col span="8">
          <p>{{userInfo.art_count}}</p>
          <p>关注</p>
        </van-col>
        <van-col span="8">
          <p>{{userInfo.fans_count}}</p>
          <p>粉丝</p>
        </van-col>
      </van-row>
    </div>

    <!--
      编辑入口
      to属性用来设置跳转路由地址
      is-link属性用来指定可点击行为
     -->
    <van-cell-group class="user-group">
      <van-cell icon="edit"
                title="编辑资料"
                to="/editUser"
                is-link />
      <van-cell icon="chat-o"
                title="小智同学"
                to="/chat"
                is-link />
      <van-cell icon="warning-o"
                title="退出登录"
                is-link
                @click="loginOut" />
    </van-cell-group>

  </div>
</template>

<script>
import { getUserInfo } from '@/api/user.js'
import { mapMutations } from 'vuex'
export default {
  name: 'UserIndex',
  data () {
    return {
      userInfo: {} // 用来储存用户基本信息
    }
  },
  methods: {
    // 映射的方式使用vuex
    ...mapMutations(['setUser']),
    hGetUserInfo () {
      getUserInfo().then(res => {
        this.userInfo = res.data.data
        // 存到vuex里一份，供其他组件使用
        this.setUser(res.data.data)
      })
    },
    loginOut () {
      this.$dialog.confirm({
        title: '提示',
        message: '确定要退出吗？'
      })
        .then(() => {
          this.$store.commit('clearToken')
          this.$router.push({
            path: '/login'
          })
        })
        .catch(() => {
          // on cancel
        })
    }
  },
  mounted () {
    this.hGetUserInfo()
  }
}
</script>

<style lang="less" scoped>
.user-container {
  position: fixed;
  top: 0;
  bottom: 0;
  width: 100%;
}
.user {
  &-profile {
    width: 100%;
    height: 200px;
    display: block;
    background: #3296fa;
    color: #fff;
    .info {
      display: flex;
      padding: 20px;
      align-items: center;
      .van-image {
        width: 64px;
        height: 64px;
      }
      .name {
        font-size: 16px;
        font-weight: normal;
        margin-left: 10px;
      }
      .van-tag {
        background: #fff;
        color: #3296fa;
      }
    }
    p {
      margin: 0;
      text-align: center;
    }
  }
  &-group {
    margin-bottom: 15px;
  }
  &-links {
    padding: 15px 0;
    font-size: 12px;
    text-align: center;
    background-color: #fff;
    .van-icon {
      display: block;
      font-size: 24px;
      padding-bottom: 5px;
    }
  }
}
</style>
